package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.linlinjava.litemall.db.dao.LitemallFormPurchaseMapper;
import org.linlinjava.litemall.db.domain.LitemallFormPurchase;
import org.linlinjava.litemall.db.domain.LitemallFormPurchaseExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;

@Service
public class LitemallPurchaseAuditService {
    @Resource
    private LitemallFormPurchaseMapper formPurchaseMapper;
    @Autowired
    private LitemallPurchaseService purchaseService;

    public List<LitemallFormPurchase> list(String status, String userType, String sort, String order, Integer page, Integer limit, boolean selectAll) {
        LitemallFormPurchaseExample example = new LitemallFormPurchaseExample();
        LitemallFormPurchaseExample.Criteria criteria = example.createCriteria();


        if (!StringUtils.isEmpty(userType)){
            criteria.andUserTypeEqualTo(Byte.valueOf(userType));
        }

        if (!StringUtils.isEmpty(status)){
            criteria.andStatusEqualTo(Byte.valueOf(status));
        }

        criteria.andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        if(!selectAll){
            PageHelper.startPage(page, limit);
        }

        return purchaseService.selectByExample(example);
    }

    public void updateById(LitemallFormPurchase purchaseExample) {

        purchaseExample.setUpdateTime(LocalDateTime.now());
        purchaseService.updateByPrimaryKeySelective(purchaseExample);
    }
    /**
     * 获取待审核总数
     *
     * @return
     */
    public int count() {
        LitemallFormPurchaseExample example = new LitemallFormPurchaseExample();
        LitemallFormPurchaseExample.Criteria criteria = example.createCriteria();
        
        criteria.andStatusEqualTo((byte)1);
        criteria.andDeletedEqualTo(false);
        return (int) formPurchaseMapper.countByExample(example);
    }

}
